package com.nineclock.system.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.nineclock.api.system.SysCompanyControllerApi;
import com.nineclock.common.constant.NcConstant;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.oss.OssProperties;
import com.nineclock.system.dto.*;
import com.nineclock.system.service.SysCompanyService;
import com.nineclock.system.service.SysCompanyUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.text.DateFormatter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/company")
@Slf4j
public class SysCompanyController implements SysCompanyControllerApi {

    @Autowired
    private SysCompanyService companyService;
    @Autowired
    private OssProperties ossProperties;
    @Autowired
    private OSS ossClient;
    @Autowired
    private SysCompanyUserService companyUserService;

    @Override
    @GetMapping("/list/{userId}")
    public Result<List<SysCompanyDTO>> queryCompanyByUserId(@PathVariable("userId") Long userId) {
        List<SysCompanyDTO> sysCompanyDTOS = companyService.queryCompanyByUserId(userId);
        return Result.success(sysCompanyDTOS);
    }



    @Override
    @GetMapping
    public Result<SysCompanyDTO> queryCompany() {
        SysCompanyDTO companyDTO = companyService.queryCompany();
        return Result.success(companyDTO);
    }


    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    @PostMapping("/uploadOSS")
    public Result uploadFile(MultipartFile file) throws IOException {
        //校验文件
        if(file == null){
            throw new NcException(ResponseEnum.UPLOAD_FILE_NOT_FOUND);
        }

        //校验文件类型
        String type = file.getContentType();
        if(!NcConstant.ALLOWED_IMG_TYPES.contains(type)){
            throw new NcException(ResponseEnum.INVALID_FILE_TYPE);
        }

        //校验文件大小
        long size = file.getSize();
        if(size > NcConstant.maxFileSize){
            throw new NcException(ResponseEnum.FILE_SIZE_EXCEED_MAX_LIMIT);
        }

        //将文件存储到OSS ------> xxxx.jpg , xxx.png
        String filename = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + "-" + file.getOriginalFilename();

        PutObjectResult putObject = ossClient.putObject(ossProperties.getBucketName(), filename, file.getInputStream());
        log.info("执行文件上传, 存储OSS , 调用结果: " + putObject);

        //组装结果并返回
        //https://nineclock-heima.oss-cn-beijing.aliyuncs.com/
        String imageUrl = ossProperties.getHost() + filename;

        return Result.success(imageUrl);
    }


    @Override
    @PutMapping
    public Result updateCompany(@RequestBody SysCompanyDTO companyDTO) {
        companyService.updateCompany(companyDTO);
        return Result.success();
    }



    @Override
    @GetMapping("/getCurrentAdmin")
    public Result<SysCompanyUserDTO> queryCurrentAdmin() {
        SysCompanyUserDTO companyUserDTO = companyUserService.queryCurrentAdmin();
        return Result.success(companyUserDTO);
    }


    @Override
    @PostMapping("/config/admin")
    public Result changeAdmin(@RequestBody SysChangeMainManagerDTO managerDTO) {
        companyService.changeAdmin(managerDTO);
        return Result.success();
    }


    @Override
    @GetMapping("/list")
    public Result<List<SysCompanyDTO>> queryByKeyword(String keyword) {
        List<SysCompanyDTO> companyDTOList = companyService.queryByKeyword(keyword);
        return Result.success(companyDTOList);
    }


    @Override
    @PostMapping("/applyJoinCompany")
    public Result applyJoinCompany(@RequestBody SysApplyJoinCompanyUserDTO companyUserDTO) {
        companyService.applyJoinCompany(companyUserDTO);
        return Result.success();
    }


    @Override
    @PostMapping("/allowedJoinCompany")
    public Result allowedJoinCompany(SysAllowedJoinCompanyUserDTO allowedDto) {
        companyService.allowedJoinCompany(allowedDto);
        return Result.success();
    }
}
